package com.redpacketcover.rc.util

import com.redpacketcover.rc.Constants

class UserInfo private constructor() {

    private var loginToken: String? = null //登录token
    private var location: String? = null //定位经纬度
    private var userType: Int? = null //账号类型
    private var userID: Int? = null //账号id

    companion object {
        private var mInstance: UserInfo? = null

        @Synchronized
        fun getInstance(): UserInfo {
            if (mInstance == null) {
                mInstance = UserInfo()
            }
            return mInstance!!
        }
    }

    init {
        loginToken = SPUtils.getInstance().getString(Constants.SP_TOKEN_LOGIN)
        location = SPUtils.getInstance().getString(Constants.SP_LOCATION)
        userType = SPUtils.getInstance().getInt(Constants.SP_USER_TYPE)
        userID = SPUtils.getInstance().getInt(Constants.SP_USER_ID)
    }

    fun setUserID(uid: Int) {
        this.userID = uid
        SPUtils.getInstance().put(Constants.SP_USER_ID, uid)
    }

    fun setUserType(type: Int) {
        this.userType = type
        SPUtils.getInstance().put(Constants.SP_USER_TYPE, type)
    }

    fun setLoginToken(loginToken: String?) {
        this.loginToken = loginToken
        SPUtils.getInstance().put(Constants.SP_TOKEN_LOGIN, loginToken ?: "")
    }

    fun setLocation(location: String?) {
        this.location = location
        SPUtils.getInstance().put(Constants.SP_LOCATION, location ?: "")
    }

    fun getUserID():Int?{
        return userID
    }

    fun getUserType(): Int? {
        return userType
    }

    fun getLoginToken(): String? {
        return loginToken
    }

    fun getLocation(): String? {
        return location
    }

    private fun empty(str: CharSequence?): Boolean {
        return str.isNullOrEmpty()
    }

    fun emptyToken(): Boolean {
        return empty(loginToken)
    }

}